<?php

namespace app\admin\controller;

class Home extends Base
{
    public function index()
    {
        return view();
    }

    public function adminList()
    {
        $admins = model('Admin')->paginate(10);
        $viewData = [
            'admins' => $admins
        ];
        $this->assign($viewData);
        return view('adminlist');
    }

    public function adminAdd()
    {
        if (request()->isAjax()) {
            $data = [
                'username' => input('username'),
                'password' => input('password'),
                'conpass' => input('conpass'),
                'status' => input('status'),
                'email' => input('email')
            ];
            $result = model('Admin')->add($data);
            if ($result == 1) {
                $this->success('账户添加成功！', 'admin/home/adminlist');
            }else {
                $this->error($result);
            }
        }
        return view('adminadd');
    }

    public function adminDisabled()
    {
        $data = [
            'id' => input('id'),
            'status' => input('status') ? 0 : 1
        ];
        $result = model('Admin')->isUpdate(true)->save($data);
        if ($result) {
            $this->success('操作成功！', 'admin/home/adminlist');
        }else {
            $this->error('操作失败！');
        }
    }

    public function adminEdit()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('id'),
                'username' => input('username'),
                'oldpass' => input('oldpass'),
                'newpass' => input('newpass'),
                'nickname' => input('nickname'),
                'status' => input('status')
            ];
            $result = model('Admin')->edit($data);
            if ($result == 1) {
                $this->success('修改成功！', 'admin/home/adminlist');
            }else {
                $this->error($result);
            }
        }
        $adminInfo = model('Admin')->find(input('id'));
        $viewData = [
            'adminInfo' => $adminInfo
        ];
        $this->assign($viewData);
        return view('adminedit');
    }

    public function adminDel()
    {
        $adminInfo = model('Admin')->find(input('id'));
        $result = $adminInfo->delete();
        if ($result) {
            $this->success('删除账号成功！', 'admin/home/adminlist');
        }else {
            $this->error('删除账号失败！');
        }
    }
}
